package com.github.aloxc.plugin.restplus.listener;

import com.github.aloxc.plugin.restplus.common.Consts;
import com.github.aloxc.plugin.restplus.entity.RestConfig;
import com.github.aloxc.plugin.restplus.entity.UserCase;
import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.github.aloxc.plugin.restplus.navigator.RestServiceDetail;
import com.github.aloxc.plugin.restplus.navigator.RestServicesNavigator;
import com.github.aloxc.plugin.restplus.utils.JsonUtils;
import com.github.aloxc.plugin.restplus.utils.StringUtils;
import com.intellij.openapi.ui.Messages;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

/**
 * 保存请求用例
 */
public class SaveAsActionListener implements ActionListener {

    private RestServiceDetail restServiceDetail;

    public SaveAsActionListener(RestServiceDetail restServiceDetail){

        this.restServiceDetail = restServiceDetail;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        String name = (String) JOptionPane.showInputDialog(
                restServiceDetail,
                I18Bundle.getMessage("com.github.aloxc.plugin.restplus.detail.usercase.saveas.message"),
                I18Bundle.getMessage("com.github.aloxc.plugin.restplus.detail.usercase.saveas.title"),
                3,
                Consts.Icons.SAVE_AS,
                null,
                ""
        );
        if (StringUtils.isNullOrEmpty(name)) {
            Messages.showMessageDialog(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.detail.usercase.saveas.dialog.message"),
                    I18Bundle.getMessage("com.github.aloxc.plugin.restplus.left.save.saveas.text"), Consts.Icons.EXCEPTION);
            return;
        }

        RestConfig config = restServiceDetail.getProject().getComponent(RestServicesNavigator.class).getConfig();
        Map<String, UserCase> userCaseMap = config.getUserCaseMap().get(
                (restServiceDetail.getParentName() != null ? restServiceDetail.getParentName() + ":" : "")
                        + restServiceDetail.getUrl());
        UserCase userCase = null;
        Map<String, Object> headerMap = JsonUtils.toBean(restServiceDetail.getHeaderTextArea().getText(), HashMap.class);
        Map<String, Object> paramMap = JsonUtils.toBean(restServiceDetail.getRequestParamsTextArea().getText(), HashMap.class);
        String test = restServiceDetail.getRequestTestTextArea().getText();
        Map<String, Object> bodyMap = null;
        if (restServiceDetail.getRequestBodyTextArea() == null || StringUtils.isNullOrEmpty(restServiceDetail.getRequestBodyTextArea().getText())) {
            bodyMap = new HashMap(2 << 3);
        } else {
            bodyMap = JsonUtils.toBean(restServiceDetail.getRequestBodyTextArea().getText(), HashMap.class);
        }

        boolean isDuplicate = false;
        if (userCaseMap == null) {
            userCaseMap = new HashMap(2 << 3);
            userCase = new UserCase();
            userCaseMap.put(UserCase.getUserCaseKey(name,restServiceDetail.getMethod()), userCase);
            config.getUserCaseMap().put((restServiceDetail.getParentName() != null ? restServiceDetail.getParentName() + ":" : "") + restServiceDetail.getUrl(), userCaseMap);
        } else {
            userCase = userCaseMap.get(UserCase.getUserCaseKey(name,restServiceDetail.getMethod()));
            if (userCase != null) {
                /*
                 * 返回用户点击的选项, 值为下面三者之一:
                 *     是:   JOptionPane.YES_OPTION
                 *     否:   JOptionPane.NO_OPTION
                 *     取消: JOptionPane.CANCEL_OPTION
                 *     关闭: JOptionPane.CLOSED_OPTION
                 */
                int result = JOptionPane.showConfirmDialog(
                        restServiceDetail,
                        I18Bundle.getMessage("com.github.aloxc.plugin.restplus.detail.usercase.saveas.duplicate.message"),
                        I18Bundle.getMessage("com.github.aloxc.plugin.restplus.detail.usercase.saveas.duplicate.level"),
                        JOptionPane.YES_NO_OPTION
                );
                if (result != JOptionPane.YES_OPTION) {
                    return;
                }
                isDuplicate = true;
            } else {
                userCase = new UserCase();
                userCaseMap.put(UserCase.getUserCaseKey(name,restServiceDetail.getMethod()), userCase);
            }
        }
        userCase.setHeaderMap(headerMap);
        userCase.setParamMap(paramMap);
        userCase.setBodyMap(bodyMap);
        userCase.setTest(test);
        config.save(restServiceDetail.getProject());

        if (!isDuplicate) {
            DefaultTreeModel model = (DefaultTreeModel) restServiceDetail.getUserCaseTree().getModel();
            DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(name);
            root.add(node);
            model.reload(root);
        }
        restServiceDetail.bindUserCaseTreeListener();
    }
}